import com.wn.domain.Person;
import org.junit.jupiter.api.Test;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author yellow Docter
 * @date 2022 -10-28
 * @desc  stream的收集
    方法        			    返回类型         				作用
    toList      			    List<T>        					把流中元素收集到List
    toSet       			    Set<T>        					把流中元素收集到Set
    toCollection   		Collection<T>     	   	            把流中元素收集到创建的集合
    counting     			Long         				   	计算流中元素的个数
    summingInt    		  Integer        			   	对流中元素的整数属性求和
 */
public class Demo06 {

    List<Person> ps = Arrays.asList(
            new Person("马云", 48, 2000d),
            new Person("马化腾", 50, 3000d),
            new Person("雷军", 46, 4000d),
            new Person("刘强东", 47, 5000d),
            new Person("张朝阳", 50, 6000d),
            new Person("张朝阳", 50, 6000d),
            new Person("张一鸣", 50, 7000d),
            new Person("张一鸣", 50, 7000d)
    );

    @Test
    public void demo01(){

        //collect 收集
        // 把流转成 Collectors.toList() list
        List<Person> collect = ps.stream().distinct().collect(Collectors.toList());
        System.out.println("collect = " + collect);

        Set<Person> collect1 = ps.stream().distinct().collect(Collectors.toSet());
        System.out.println("collect1 = " + collect1);

    }

    @Test
    public void demo02(){
        // 把流进行操作 放到一个 new的 Collection的集合中
        HashSet<Person> collect = ps.stream().distinct().collect(Collectors.toCollection(() -> new HashSet<>()));
        System.out.println("collect = " + collect);

    }

    @Test
    public void demo03(){
        //总数
        // Long collect = ps.stream().distinct().collect(Collectors.counting());
        Long collect = ps.stream().distinct().count();
        System.out.println("collect = " + collect);

        Integer collect1 = ps.stream().distinct().collect(Collectors.summingInt(p -> p.getAge()));
//        int sum = ps.stream().distinct().mapToInt(类::方法名称).sum();

        int sum = ps.stream().distinct().mapToInt(Person::getAge).sum();
        System.out.println("collect1 = " + collect1);

    }
    
}
